package com.suray.wcs.res.msgparse;

import com.suray.wcs.res.enums.MoveOrder;
import com.suray.wcs.res.service.rescontact.ActionOrder;
import com.suray.wcs.res.service.rescontact.MapDataModel;

public class ByteBeanUtil {
    /**
     * 对象装换成字节数组发送到res
     * @param obj
     * @param rgvNo 小车编号
     * @return
     */
    public static byte[] toByte(Object obj, Integer rgvNo) {
        //行驶命令
        int taskNoValue = 0;
        int taskTypeValue = 0;
        int segmentNoValue = 0;
        byte[] coordListValue = null;
        //动作命令
        int cmdNoValue = 0;
        int cmdValue = 0;
        int cmdParamValue = 0;

        if (obj instanceof HeartSendBean) {
            HeartSendBean heartSendBean = (HeartSendBean) obj;
            MoveOrder moveOrder = heartSendBean.getMoveCommand().poll();
            if (moveOrder != null) {
                taskNoValue = moveOrder.getTaskNo();
                taskTypeValue = moveOrder.getTaskType();
                segmentNoValue = moveOrder.getSegmentNo();
                if (moveOrder.getNodeList() != null) {
                    coordListValue = MsgTransferUtil.parseLocation(moveOrder.getNodeList());
                }
            }
            ActionOrder actionOrder = heartSendBean.getActionCommand().poll();
            if (actionOrder != null) {
                cmdNoValue = actionOrder.getCommandNo();
                cmdValue = actionOrder.getCommand();
                cmdParamValue = actionOrder.getCommandParameter();
            }
            return new SendMsgHeartBeat(heartSendBean.getDeviceType(), rgvNo, heartSendBean.getMode(),
                    heartSendBean.getMapVersion(), taskNoValue, taskTypeValue, segmentNoValue, coordListValue, cmdNoValue, cmdValue,
                    cmdParamValue, heartSendBean.getAreaNum(), heartSendBean.getAuthority()).makeMsg();
        } else if (obj instanceof MapDataModel) {
            MapDataModel mapDataModel = (MapDataModel) obj;
            return new SendMsgMapData(1,mapDataModel.getDeviceNo(), mapDataModel.getMode(), mapDataModel.getData()).makeMsg();
        } else {
            throw new RuntimeException("不合法的对象" + obj);
        }
    }

    /**
     * RES返回结果的数组转换成对象
     *
     * @param bs
     * @return
     */
    public static ResultBase parseByte(byte[] bs) {
        int mode = MsgTransferUtil.getValues(bs, 48, 8);
        //返回模式也是8位，方便解析
        switch (mode) {
            case 0:
                ReceiveMsgHeartBeatBase msgData = new ReceiveMsgHeartBeatBase(bs);
                return new HeartBaseResult(msgData.deviceType, msgData.deviceNo, msgData.mode,
                        msgData.isSameMapVersion, msgData.getCurrentLocation(), msgData.getCurentSegment(),
                        msgData.getSegmentLocation(), msgData.getCurBarcode(), msgData.getCarStatus(),
                        msgData.getPalletStatus(), msgData.getReserveStatus(), msgData.getDriveDirection(),
                        msgData.getElectric(), msgData.getStatusMessage(),msgData.barrierAtOne,msgData.barrierAtTwo,msgData.barrierAtThree,
                        msgData.barrierAtFour,msgData.offsetGroundCode, msgData.isRight(), msgData.taskNo,
                        msgData.taskType, msgData.taskResult, msgData.cmdNo,
                        msgData.cmdExecuteResult, msgData.cmdExecuteResultParam);
            case 2:
                ReceiveMsgMapData receiveMsgMapData = new ReceiveMsgMapData(bs);
                return new MapDataResult(receiveMsgMapData.getDeviceNo(), receiveMsgMapData.getMode(), receiveMsgMapData.getAck(), receiveMsgMapData.isRight());
            default:
                throw new RuntimeException("当前模式不合法:" + mode);
        }

    }

    /**
     * RES返回结果的数组转换成对象
     *
     * @param bs
     * @return
     */
    public static HeartBaseResult parseByteToHeartBaseResult(byte[] bs) {
        int mode = MsgTransferUtil.getValues(bs, 48, 8);
        if(mode == 0){
            ReceiveMsgHeartBeatBase msgData = new ReceiveMsgHeartBeatBase(bs);
            return new HeartBaseResult(msgData.deviceType, msgData.deviceNo, msgData.mode,
                    msgData.isSameMapVersion, msgData.getCurrentLocation(), msgData.getCurentSegment(),
                    msgData.getSegmentLocation(), msgData.getCurBarcode(), msgData.getCarStatus(),
                    msgData.getPalletStatus(), msgData.getReserveStatus(), msgData.getDriveDirection(),
                    msgData.getElectric(),msgData.statusMessage, msgData.barrierAtOne,msgData.barrierAtTwo,msgData.barrierAtThree,
                    msgData.barrierAtFour,msgData.offsetGroundCode, msgData.isRight(), msgData.taskNo,
                    msgData.taskType, msgData.taskResult, msgData.cmdNo,
                    msgData.cmdExecuteResult, msgData.cmdExecuteResultParam);
        }else {
            return null;
        }
    }

    /**
     * RES返回结果的数组转换成对象
     *
     * @param bs
     * @return
     */
    public static MapDataResult parseByteMap(byte[] bs) {
        ReceiveMsgMapData receiveMsgMapData = new ReceiveMsgMapData(bs);
        return new MapDataResult(receiveMsgMapData.getDeviceNo(), receiveMsgMapData.getMode(), receiveMsgMapData.getAck(), receiveMsgMapData.isRight());
    }
}
